{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "27b5be67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing tang.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile tang.txt\n",
    "hello python\n",
    "tang yu di \n",
    "jin tian tian qi  bu cuo\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d5b2dbde",
   "metadata": {},
   "outputs": [],
   "source": [
    "txt = open('./data/tang.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f7e882de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello python\n",
      "tang yu di \n",
      "jin tian tian qi  bu cuo\n",
      "\n"
     ]
    }
   ],
   "source": [
    "txt_read =txt.read()\n",
    "print(txt_read)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "578e48fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "['hello python\\n', 'tang yu di \\n', 'jin tian tian qi  bu cuo\\n']\n"
     ]
    }
   ],
   "source": [
    "lines =txt.readlines()\n",
    "print(type(lines))\n",
    "print(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f290d0e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cur_line: hello python\n",
      "\n",
      "cur_line: tang yu di \n",
      "\n",
      "cur_line: jin tian tian qi  bu cuo\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for line in lines:\n",
    "    print('cur_line:',line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5a90cf62",
   "metadata": {},
   "outputs": [],
   "source": [
    "txt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "daf3c6e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "txt = open('tang_write.txt',\"w\")\n",
    "txt.write(\"jin tian tian qi bu cuo\")\n",
    "txt.write(\"\\n\")\n",
    "txt.write(\"tang yu di\")\n",
    "txt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bc9fc734",
   "metadata": {},
   "outputs": [],
   "source": [
    "txt = open('tang_write.txt',\"w\")\n",
    "txt.write(\"123\")\n",
    "txt.write(\"\\n\")\n",
    "txt.write(\"456\")\n",
    "txt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fa294f3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "txt = open('tang_write.txt',\"a\")\n",
    "txt.write(\"tangyudi\")\n",
    "txt.write(\"\\n\")\n",
    "txt.write(\"789\")\n",
    "txt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "90669935",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "\n"
     ]
    }
   ],
   "source": [
    "txt = open('tang_write.txt',\"w\")\n",
    "for i in range(5):\n",
    "    txt.write(str(i)+'\\n')\n",
    "txt2 = open('tang_write.txt','r')\n",
    "txt.close()\n",
    "print(txt2.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "29d5f3a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "error: 50\n"
     ]
    }
   ],
   "source": [
    "txt = open('tang_write.txt',\"w\")\n",
    "try:\n",
    "    for i in range(100):\n",
    "        10/(i-50)\n",
    "        txt.write(str(i)+\"\\n\")\n",
    "except Exception:\n",
    "    print('error:',i)\n",
    "finally:\n",
    "    txt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4aa5a16",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('tang_write.txt',\"w\") as f:\n",
    "    f.write(\"jin tian tian qi bu cuo \")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
